﻿Public Class frmUserEntry

    Public _UserID As String
    Public _isEmployee As Boolean

    Private Sub frmUserEntry_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        getComboLawyer()
        getComboEmployee()
        reset()
        If _UserID <> "" Then
            getTextbox()
            gbCreateUserFor.Enabled = False
        End If
    End Sub

    Protected Sub getTextbox()
        Dim dt As New DataTable
        Dim oUser As New cUser
        dt = oUser.select_User_byID(_UserID)
        If _isEmployee Then
            rdEmployee.Checked = True
            cboEmployee.SelectedValue = dt.Rows(0)("EmployeeID")
            txtEmployeeUser.Text = dt.Rows(0)("UserID")
            txtEmployeePass.Text = dt.Rows(0)("Password")
            cboEmployeeRole.Text = dt.Rows(0)("Role")
        Else
            rdLawyer.Checked = True
            cboLawyer.SelectedValue = dt.Rows(0)("LawyerID")
            txtLawyerUser.Text = dt.Rows(0)("UserID")
            txtLawyerPass.Text = dt.Rows(0)("Password")
            cboLawyerRole.Text = dt.Rows(0)("Role")
        End If
    End Sub

    Public Sub getComboLawyer()
        Dim oControl As New cControl
        oControl.getCombo(cboLawyer, "SELECT LawyerID,LawyerName FROM tblLawyer WHERE Active=1", "LawyerName", "LawyerID")
    End Sub

    Public Sub getComboEmployee()
        Dim oControl As New cControl
        oControl.getCombo(cboEmployee, "SELECT EmployeeID,EmployeeName FROM tblEmployee WHERE Active=1", "EmployeeName", "EmployeeID")
    End Sub

    Private Sub ShowForm(ByVal frm As Form)
        frm.MdiParent = Me.MdiParent
        frm.ControlBox = False
        frm.ShowIcon = False
        frm.WindowState = FormWindowState.Maximized
        Me.Close()
        frm.Show()
        frm.BringToFront()
    End Sub

    Protected Sub reset()
        rdLawyer.Checked = True
        rdEmployee.Checked = False
        gbLawyer.Enabled = True
        gbEmployee.Enabled = False

        If cboLawyer.Items.Count > 0 Then
            cboLawyer.SelectedIndex = 0
        Else
            cboLawyer.SelectedIndex = -1
        End If
        txtLawyerUser.Clear()
        txtLawyerPass.Clear()
        If cboLawyerRole.Items.Count > 0 Then
            cboLawyerRole.SelectedIndex = 0
        Else
            cboLawyerRole.SelectedIndex = -1
        End If

        If cboEmployee.Items.Count > 0 Then
            cboEmployee.SelectedIndex = 0
        Else
            cboEmployee.SelectedIndex = -1
        End If
        txtEmployeeUser.Clear()
        txtEmployeePass.Clear()
        If cboEmployeeRole.Items.Count > 0 Then
            cboEmployeeRole.SelectedIndex = 0
        Else
            cboEmployeeRole.SelectedIndex = -1
        End If
    End Sub

    Protected Sub clear_Lawyer()
        If cboLawyer.Items.Count > 0 Then
            cboLawyer.SelectedIndex = 0
        Else
            cboLawyer.SelectedIndex = -1
        End If
        txtLawyerUser.Clear()
        txtLawyerUser.Focus()
        txtLawyerPass.Clear()
        If cboLawyerRole.Items.Count > 0 Then
            cboLawyerRole.SelectedIndex = 0
        Else
            cboLawyerRole.SelectedIndex = -1
        End If
    End Sub

    Protected Sub clear_Employee()
        If cboEmployee.Items.Count > 0 Then
            cboEmployee.SelectedIndex = 0
        Else
            cboEmployee.SelectedIndex = -1
        End If
        txtEmployeeUser.Clear()
        txtEmployeeUser.Focus()
        txtEmployeePass.Clear()
        If cboEmployeeRole.Items.Count > 0 Then
            cboEmployeeRole.SelectedIndex = 0
        Else
            cboEmployeeRole.SelectedIndex = -1
        End If
    End Sub

    Public Sub EnableControl()
        If _isEmployee Then
            txtEmployeeUser.Enabled = False
            txtEmployeePass.Enabled = False
            cboEmployee.Enabled = False
            cboEmployeeRole.Enabled = False
            btnAddEmployee.Enabled = False
            gbLawyer.Visible = False
            gbEmployee.Visible = True
        Else
            txtLawyerUser.Enabled = False
            txtLawyerPass.Enabled = False
            cboLawyer.Enabled = False
            cboLawyerRole.Enabled = False
            btnAddLawyer.Enabled = False
            gbLawyer.Visible = True
            gbEmployee.Visible = False
        End If
        gbCreateUserFor.Enabled = False
        btnSave.Enabled = False
        btnList.Enabled = False
        btnClean.Enabled = False
    End Sub

    Private Sub rdLawyer_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdLawyer.CheckedChanged
        RadioCheck()
        clear_Employee()
    End Sub

    Private Sub rdEmployee_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rdEmployee.CheckedChanged
        RadioCheck()
        clear_Lawyer()
    End Sub

    Protected Sub RadioCheck()
        If rdLawyer.Checked = True Then
            gbLawyer.Enabled = True
            gbEmployee.Enabled = False
        Else
            gbLawyer.Enabled = False
            gbEmployee.Enabled = True
        End If
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Close()
    End Sub

    Private Sub btnClean_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClean.Click
        reset()
    End Sub

    Private Sub btnList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnList.Click
        If Application.OpenForms().OfType(Of frmUser).Any Then
            frmUser.getUser()
            Close()
        Else
            ShowForm(frmUser)
        End If
    End Sub

    Protected Function CheckNull_Lawyer() As Boolean
        Dim oc As New cControl
        If oc.checknull(txtLawyerUser.Text) And oc.checknull(txtLawyerPass.Text) And oc.checknull(cboLawyerRole.Text) And oc.checknull(cboLawyer.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Protected Function CheckNull_Employee()
        Dim oc As New cControl
        If oc.checknull(txtEmployeeUser.Text) And oc.checknull(txtEmployeePass.Text) And oc.checknull(cboEmployeeRole.Text) And oc.checknull(cboEmployee.Text) Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If _UserID <> "" Then
            updated()
        Else
            inserted()
        End If
    End Sub

    Protected Sub inserted()
        Dim oUser As New cUser
        If rdLawyer.Checked = True Then
            If CheckNull_Lawyer() Then
                If oUser.insert_User(txtLawyerUser.Text, txtLawyerPass.Text, cboLawyerRole.Text, DBNull.Value, cboLawyer.SelectedValue) Then
                    MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    clear_Lawyer()
                Else
                    MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Else
            If CheckNull_Employee() Then
                If oUser.insert_User(txtEmployeeUser.Text, txtEmployeePass.Text, cboEmployeeRole.Text, cboEmployee.SelectedValue, DBNull.Value) Then
                    MessageBox.Show("រក្សាទុកបានសម្រេច!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    clear_Employee()
                Else
                    MessageBox.Show("រក្សាទុកមិនបានសម្រេចទេ", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "រក្សាទុក", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Protected Sub updated()
        Dim oUser As New cUser
        If rdLawyer.Checked = True Then
            If CheckNull_Lawyer() Then
                If oUser.update_User(txtLawyerUser.Text, txtLawyerPass.Text, cboLawyerRole.Text, DBNull.Value, cboLawyer.SelectedValue) Then
                    MessageBox.Show("កែប្រែបានសម្រេច!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    reset()
                    frmUser.getUser()
                    Close()
                Else
                    MessageBox.Show("កែប្រែមិនបានសម្រេចទេ", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        Else
            If CheckNull_Employee() Then
                If oUser.update_User(txtEmployeeUser.Text, txtEmployeePass.Text, cboEmployeeRole.Text, cboEmployee.SelectedValue, DBNull.Value) Then
                    MessageBox.Show("កែប្រែបានសម្រេច!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
                    reset()
                    frmUser.getUser()
                    Close()
                Else
                    MessageBox.Show("កែប្រែមិនបានសម្រេចទេ", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Error)
                End If
            Else
                MessageBox.Show("សូមបញ្ចូលគ្រប់ពត៌មានទាំងអស់!", "កែប្រែ", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
        End If
    End Sub

    Private Sub btnAddLawyer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddLawyer.Click
        frmLawyerEntry.btnList.Enabled = False
        frmLawyerEntry._isOtherForm = True
        frmLawyerEntry.ShowDialog()
    End Sub

    Private Sub btnAddEmployee_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddEmployee.Click
        frmEmployeeEntry.btnList.Enabled = False
        frmEmployeeEntry._isOtherForm = True
        frmEmployeeEntry.ShowDialog()
    End Sub
End Class